package register
class ControllerBase {

    def defaultAction = "list"
    
//    def login = {
//        session.userId = null
//        User user = User.authenticate(params.name, params.password)
//        if (user) {
//            session.userId = user.id
//            session.albumsCount = user.albumsCount
//        }
//        else {
//            flash.loginNotice = message(code: 'intercept.invalid.login')
//        }
//        redirect(url: params.url)
//    }
//  
//    def logout = {
//        session.userId = null
//        session.albumId = null
//        session.albumsCount = null
//        redirect(controller: 'user', action: 'list')
//    }

    def actionMethods() {
        def methods = null
        switch (actionName) {
            case 'login':
                methods = [ 'POST', 'PUT' ]
                break;
            case 'create':
                methods = [ 'GET' ]
                break;
            case 'delete':
                methods = [ 'POST', 'DELETE' ]
                break;
            case 'update':
            case 'save':
                methods = [ 'POST', 'PUT' ]
                break;
        }
        methods
    }

    def validMethod(acceptable) {
        def method = request.method
        acceptable.find { it == method } ? null : message(code: 'Illegal operation')
    }

    def loggedInUser() {
        if (!session.userId) {
            return message(code: 'intercept.not.loggedin')
        }
        null
    }

    Owner sessionOwner() {
        if (session.ownerId) {
            return Owner.get(session.ownerId)
        }
        null
    }

    void prepareList() {
        if (!params.max) {
            params.max = 10
        }
        if (!params.order) {
            params.order = "desc"
        }
        if (!params.sort) {
            params.sort = "id"
        }
    }
}